package com.drought.rained.presenter;

import android.graphics.Bitmap;

import com.drought.rained.business.CustomConsumerLJ;
import com.drought.rained.business.ParamsFactoryLJ;
import com.drought.rained.cache.UserInfoLJ;
import com.drought.rained.mvpview.ImgMvpViewLJ;
import com.drought.rained.response.IdentityResponseLJ;
import com.drought.rained.utils.ImageUtilsLJ;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class ImgPresenterLJ extends BasePresenterLJ {
    private ImgMvpViewLJ mvpView;

    public ImgPresenterLJ(ImgMvpViewLJ mvpView) {
        this.mvpView = mvpView;
    }

    public void uploadIdentityCard(File file) {

        Map<String, Object> fromMap = new HashMap<>();
        fromMap.put("userUuid", UserInfoLJ.getSaveUuid());
        fromMap.put("fileType", 1);

        HashMap<String, Object> map = new HashMap<>();
        map.put("paramStr", ParamsFactoryLJ.getCommonParamsStr(fromMap));

        RequestBody requestBody = FormBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "file", requestBody);

        toSubScribe(req.uploadIdentityCard(UserInfoLJ.getToken(), map, filePart), new CustomConsumerLJ<IdentityResponseLJ>() {
            @Override
            public void call(IdentityResponseLJ identityResponse) {
                mvpView.onGetDataNext(identityResponse, 1);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void uploadImg(int type, File file) {

        Map<String, Object> fromMap = new HashMap<>();
        fromMap.put("userUuid", UserInfoLJ.getSaveUuid());
        fromMap.put("fileType", type);

        HashMap<String, Object> map = new HashMap<>();
        map.put("paramStr", ParamsFactoryLJ.getCommonParamsStr(fromMap));

        RequestBody requestBody = FormBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "file", requestBody);

        toSubScribe(req.uploadSingleImg(UserInfoLJ.getToken(), map, filePart), new CustomConsumerLJ<IdentityResponseLJ>() {
            @Override
            public void call(IdentityResponseLJ identityResponse) {
                mvpView.onGetDataNext(identityResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    /**
     * 通过bitmap上传身份证
     *
     * @param bitmap
     */
    public void uploadIdentityCardForBitmap(final int type,Bitmap bitmap) {
        Map<String, Object> fromMap = new HashMap<>();
        fromMap.put("userUuid", UserInfoLJ.getSaveUuid());
        fromMap.put("fileType", type);
        HashMap<String, Object> map = new HashMap<>();
        map.put("paramStr", ParamsFactoryLJ.getCommonParamsStr(fromMap));
        byte[] bitmapByte = ImageUtilsLJ.bitmap2Bytes(bitmap, Bitmap.CompressFormat.JPEG);
        RequestBody requestBody = FormBody.create(MediaType.parse("multipart/form-data"),
                bitmapByte);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "file",
                requestBody);

        toSubScribe(req.uploadIdentityCard(UserInfoLJ.getToken(), map, filePart), new CustomConsumerLJ<IdentityResponseLJ>() {
            @Override
            public void call(IdentityResponseLJ identityResponse) {
                mvpView.onGetDataNext(identityResponse, 1);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }
}
